from django import template
from user import * 
from glue.forum import *

register = template.Library()

def HistoryExplain(hs):
  if (hs.type == "POST_REMOVE"):
    return hs.who + " removed a post: '" + hs.para + "'"
  elif (hs.type == "POST_ADD"):
    return hs.who + " added a new post: '" + hs.para + "'"
  elif (hs.type == "IMAGE_ADD"):
    return hs.who + " added a new image in gallery: '" + hs.para + "'"
  elif (hs.type == "GIRL_ADD"):
    return hs.who + " added girl info: '" + hs.para + "'"
  elif (hs.type == "GIRL_REMOVE"):
    return hs.who + " removed girl info: '" + hs.para + "'"
  elif (hs.type == "REPORT"):
    plist = json.loads(hs.para)
    [address,lat,lon,t] = plist
    return hs.who + " shared his experience of %s in %s"%(unquote(t),hs.service)
 
def HistoryExplainXML(hs):
  if(hs.type == "REPORT"):
    plist = json.loads(hs.para)
    [address,lat,lon,t] = plist
    ret_str = """<REPORT name='%s' who='%s' type='%s'><latlong>%d,%d</latlong><address>%s</address></REPORT>"""%(hs.service,hs.who,t,lat,lon,address)
    return ret_str
  else:
    return "<REF/>"
 

@register.filter("hash")
def hash(dict,key):
  "Return the value of a key in a dictionary. "
  if(dict and dict.has_key(key)):
    return dict[key]
  else:
    return None

@register.filter("hashout")
def hash(dict,key):
  "Return the value of a key in a dictionary. "
  if(dict and dict.has_key(key)):
    v = dict[key]
    del dict[key]
    return v 
  else:
    return None


@register.filter("attr")
def attr(obj,key):
  "Return the value of a key in a obj. "
  if (obj):
    if(obj.hasattr(key)):
      return obj.getattr(key)
    else:
      return None
  else:
    return None

@register.filter("informal")
def attr(obj):
    return obj.replace('-',' ') 

@register.filter("hs_explain")
def attr(obj):
    return HistoryExplain(obj)

@register.filter("hs_explain_xml")
def attr(obj):
    return HistoryExplainXML(obj)

@register.filter("date")
def date(obj,key):
    if(obj.find(key) == -1):
        return "";
    else:
        return obj;

@register.filter("servername")
def servername(name):
    return name.replace('-',' ');

@register.filter("capitalize")
def capitalize(name):
    tokens = name.split('-');
    newname='';
    for i in tokens: 
      newname=newname + i.capitalize() + ' ';
    return newname;

@register.filter("localname")
def localname(name):
    return name.replace(' ','-');

@register.filter("regionname")
def regionname(name):
    if(cmp(str(name),'None') == 0):
      return '';
    else:
      return name;

@register.filter("keywordsname")
def keywordsname(name):
    if(cmp(str(name),'None') == 0):
      return '';
    else:
      return name;

@register.filter("phoneformat")
def phoneformat(name):
    if(len(name) == 10):
      if(cmp(name[0:2],'04') == 0):
        return name[0:4] + ' ' + name[4:7] + ' ' + name[7:10];
      else:
        return name[0:2] + ' ' + name[2:6] + ' ' + name[6:10];
    else:
      return name;

@register.simple_tag
def NoAuthorityWarning():
    return 'You have no authority to configure this adult service shop!'
